package edu.hunau.cxb19.oop.class22;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.IntPredicate;
import java.util.function.IntSupplier;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.ToIntBiFunction;

import org.junit.Test;

/**
 *
 * @author hb
 *
 *         2020-03-25 14:55:50
 */
public class FunInterfaceTest {

	@Test
	public void predicateTest() {
		List<String> datas = new ArrayList<String>();
		Collections.addAll(datas, "a01.txt", "b01.pdf", "a02.txt", "b02.doc", "c01.xml");

		// 获得所有的文本文档
		Predicate<String> p1 = t -> t.endsWith(".txt");
		List<String> subList = FilterUtils.filter(datas, p1);

		System.out.println(subList);

	}

	@Test
	public void consumerTest() {
		List<String> datas = new ArrayList<String>();
		Collections.addAll(datas, "a01.txt", "b01.pdf", "a02.txt", "b02.doc", "c01.xml");
		// 使用Consumer接口将集合中的数据迭代输出
		FilterUtils.forEach(datas, t -> System.out.println(t));
		FilterUtils.forEach(datas, System.out::println);
	}

	@Test
	public void functionTest() {
		List<Apple> datas = Apple.getDatas();
		// 获得所有的苹果的重量
		List<Integer> results = FilterUtils.map(datas, t -> t.getWeight());
		System.out.println(results);
	}

	@Test
	public void supplierTest() {

		Supplier<Apple> s1 = () -> new Apple();
		Apple apple = s1.get();
		System.out.println(apple);
	}

	@Test
	public void testBoxing() {
		// 在底层进行计算的过程中需要使用拆箱
		Predicate<Integer> p1 = (Integer i) -> i % 2 == 0;
		// 底层不会使用拆箱
		IntPredicate p2 = (int i) -> i % 2 == 0;

		List<Integer> list = new ArrayList<>();
		Collections.addAll(list, 1, 2, 3, 4, 556, 575, 3, 435, 234, 234, 56, 23, 4);

		FilterUtils.filter(list, p1);
		// FilterUtils.filter(list, p2);

	}

	@Test
	public void mulitToOne() {
		// 为Apple提供了默认的比较规则，根据苹果的重量进行比较
		BiFunction<Apple, Apple, Integer> b1 = (Apple a1, Apple a2) -> a1.compareTo(a2);

		ToIntBiFunction<Apple, Apple> b2 = (Apple a1, Apple a2) -> a1.compareTo(a2);
	}

	public void methodCallTest() {
		List<String> datas = new ArrayList<String>();
		Collections.addAll(datas, "123", "23", "234", "234");
		// 将List<String>转成List<Integer>
		List<Integer> result = FilterUtils.map(datas, (t) -> Integer.parseInt(t));
		result = FilterUtils.map(datas, Integer::parseInt);

		// 将List<String>根据字符串的长度转成List<Integer>
		FilterUtils.map(datas, (t) -> t.length());
		FilterUtils.map(datas, String::length);

		Random r = new Random();
		IntSupplier is1 = () -> r.nextInt();

		IntSupplier is2 = r::nextInt;
	}

	@Test
	public void constructCallTest() {
		Supplier<Apple> a1 = () -> new Apple();
		Supplier<Apple> a2 = Apple::new;

		// 需要通过Apple(weight)实例化一个Apple的实例。
		Function<Integer, Apple> a3 = (Integer w) -> new Apple(w);
		Function<Integer, Apple> a4 = Apple::new;
		Apple apple = a4.apply(100);
		System.out.println(apple);
		
		//将List<Integer>苹果的重量转成List<Apple>
		List<Integer> list = new ArrayList<>();
		Collections.addAll(list, 100, 202, 301, 410);
		List<Apple> apples1 = FilterUtils.map(list, (t)->new Apple(t));
		List<Apple> apples2 = FilterUtils.map(list, Apple::new);

	}
}
